package com.hnqcgczy.supplier.controller;

import com.hnqcgczy.supplier.service.AdminService;
import com.hnqcgczy.supplier.service.impl.AdminServiceImpl;
import com.hnqcgczy.supplier.utils.BaseServlet;
import com.hnqcgczy.supplier.vo.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//value是虚拟路径
@WebServlet(name = "AdminServlet", value = "/AdminServlet")
public class AdminServlet extends BaseServlet {
    //Servlet是通过Service来进行业务处理
    private AdminService adminService = new AdminServiceImpl();

    /**
     * 登陆
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //获取用户名和密码
        String adminName = request.getParameter("adminName");
        String adminPassword = request.getParameter("adminPassword");
        Admin admin = adminService.login(adminName, adminPassword);
        //判断是否为空
        if (admin == null) {
            //用户名或密码错误，回到登陆页，重定向，还有一种就是转发
            response.sendRedirect("login.jsp");
        } else {
            //正常
            //传值（装货）
            request.getSession().setAttribute("admin", admin);
            //开车，开到目的地，主页面
            //request.getRequestDispatcher("AdminServlet").forward(request, response);
            response.sendRedirect("AdminServlet");

        }

    }

    // AdminServlet?type=reg
    public void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前台传过来的数,并且封装到实体对象中
        Admin admin = new Admin();
        admin.setAdminName(request.getParameter("adminName") != null ? request.getParameter("adminName") : "");
        admin.setAdminPassword(request.getParameter("adminPassword") != null ? request.getParameter("adminPassword") : "");
        admin.setPhone(request.getParameter("phone") != null ? request.getParameter("phone") : "");
        admin.setEmail(request.getParameter("email") != null ? request.getParameter("email") : "");
        boolean flag = adminService.reg(admin);
        //跳转，传值
        response.sendRedirect("AdminServlet");

    }

    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //前台是有根据id和用户名做了查询功能的
        //获取用户
        String adminName = request.getParameter("adminName") != null ? request.getParameter("adminName") : "";
        String adminId = request.getParameter("adminId") != null ? request.getParameter("adminId") : "0";

        //如果开发中遇到这种状况怎么办？以开发文档为准
        Admin admin = new Admin();
        admin.setAdminId(Integer.parseInt(adminId));
        admin.setAdminName(adminName);
        List<Admin> admins = adminService.queryAdmin(admin);
        //装货
        request.getSession().setAttribute("admins", admins);
        //送货
        request.getRequestDispatcher("UserControl.jsp").forward(request, response);

    }

    //
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //根据id
        String adminId = request.getParameter("adminId") != null ? request.getParameter("adminId") : "0";
        boolean flag = adminService.deleteById(Integer.parseInt(adminId));
        //跳转，传值
        response.sendRedirect("AdminServlet");
    }

    //
    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Admin admin = new Admin();
        admin.setAdminId(request.getParameter("adminId") != null ? Integer.parseInt(request.getParameter("adminId")) : 0);
        admin.setAdminName(request.getParameter("adminName"));
        admin.setAdminPassword(request.getParameter("adminPassword"));
        admin.setAdminRole(request.getParameter("adminRole") != null ? Integer.valueOf(request.getParameter("adminRole")) : -1);
        admin.setPhone(request.getParameter("phone"));
        admin.setEmail(request.getParameter("email"));
        boolean flag = adminService.updateById(admin);
        //跳转，传值
        response.sendRedirect("AdminServlet");
    }

}
